const cloud = require('wx-server-sdk')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })

exports.main = async (event, context) => {
  const db = cloud.database()
  const wxContext = cloud.getWXContext()
  
  try {
    const result = await db.collection('account').add({
      data: {
        icon: event.icon,
        name: event.name,
        balance: event.balance,
        remark: event.remark,
        after: event.after|| 0.0,
        createTime: db.serverDate(),
        updateTime: db.serverDate(),
        _openid: wxContext.OPENID || event.openid
      }
    })
    return {
      code: 0,
      data: result,
      message: '保存成功'
    }
  } catch (err) {
    return {
      code: -1,
      error: err,
      message: '保存失败'
    }
  }
}